<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24/024
 * Time: 20:37
 */

namespace app\admin\model;

class Role extends \think\Model
{

    protected $alias = 'r'; //表别名

    /**
     * 获取单条
     * @param $id
     * @return array|null|\PDOStatement|string|\think\Model
     */
    public function _findOne($id){
        return $this->where([['id', '=', $id]])->find();
    }


    /**
     * 获取角色
     * @param bool $is_json: 1搜索-自动完成
     * @return array|string
     */
    public function _getRoles($is_json = true){
        $result = $this->field('id,role_name')->select();
        if(!$result){
            $result = [];
        }
        $result = $result->toArray();
        if($is_json){
            $result = json_encode($result, JSON_UNESCAPED_UNICODE);
        }
        /*else{
            $data = [];
            foreach ($result as $k => $v){
                $data[$v['id']] = $v['name'];
            }
            $result = $data;
        }*/
        return $result;
    }
    




    /**
     * 列表
     * @param array $condition
     * @param int $page_size
     * @param string $order_by
     * @return \think\Paginator
     */
    public function _list($condition = [], $page_size = PAGE_SIZE_ADMIN, $order_by = 'sort desc'){
        $obj = $this->order($order_by);
        $result = get_page($obj, $condition, $page_size);
       
        return $result;
    }


    /**
     * 添加
     * @param $post
     * @return bool
     */
    public function _create($post){
        $insert = [
            'role_name' => trim($post['name']),
            'role_summary' => trim($post['desc']),
            'auth_ids' => trim($post['auth_ids']),
            'updated' => time(),
            'created' => time(),
            
        ];
        if(!$this->save($insert)){
            $this->error = '操作失败';
            return false;
        }
        return true;
    }


    /**
     * 详情
     * @param $id
     * @return array|bool
     */
    public function _detail($id){
        $result = $this->_findOne($id)->toArray();
        if(empty($result)){
            $this->error = '没有此角色';
            return false;
        }
        $result['auth_ids'] = explode(',', $result['auth_ids']);
        return $result;
    }


    /**
     * 修改
     * @param $post
     * @return bool
     */
    public function _modify($post){
        $role = $this->_findOne($post['id']);
        
        if(empty($role)){
            $this->error = '没有此角色';
            return false;
        }
        $update = [
            'role_name' => trim($post['name']),
            'role_summary' => trim($post['desc']),
            'auth_ids' => trim($post['auth_ids']),
            'updated' => time(),
        ];
        $condition = [
            'id' => $post['id']
        ];
        $ret = $this->save($update, $condition);
        if($ret === false){
            $this->error = '操作失败';
            return false;
        }
        return true;
    }


    /**
     * 删除
     * @param $id
     * @return bool
     */
    public function _remove($id){
        $role = $this->_findOne($id);
        if(!$role){
            $this->error = '没有此角色';
            return false;
        }
        
        if($role->is_can_del == -1){
            $this->error = '该角色不能删除';
            return false;
        }
        if(!$role->delete()){
            $this->error = '操作失败';
            return false;
        }
        return true;
    }

    /**
    * 数据总条数
    */
    public function _count($where = []){
        return $this->alias('r')->where($where)->count();
    }

}